﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class ChuongTrinhDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from ChuongTrinh");
        }

        public ChuongTrinh GetById(String maCT)
        {
            ChuongTrinh ct = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from ChuongTrinh where MaCT = '" + maCT + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    ct = new ChuongTrinh();
                    ct.MaCT = dataReader.GetString(0);
                    ct.TenCT = dataReader.GetString(1);
                    ct.TongThoiLuongPS = dataReader.GetInt32(2);
                    ct.MaNSX = dataReader.GetString(3);
                    ct.MaLoaiCT = dataReader.GetString(4);
                    ct.Status = dataReader.GetBoolean(5);
                    return ct;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return ct;
        }

        public bool Insert(ChuongTrinh ct)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[ChuongTrinh]([MaCT],[TenCT],[TongThoiLuongPS],[MaNSX],[MaLoaiCT],[Status])"
               + "VALUES('" + ct.MaCT + "','" + ct.TenCT + "'," + ct.TongThoiLuongPS + ",'" + ct.MaNSX + "','" + ct.MaLoaiCT + "'," + ct.Status + ")";
            return Update(sql);
        }

        public bool DeleteById(String mact)
        {
            return Update("delete from ChuongTrinh where MaCT = '" + mact + "'");
        }

        public bool UpdateById(ChuongTrinh ct)
        {
            String sql = "update ChuongTrinh "
                 + " set TenCT = '" + ct.TenCT + "'"
                 + " set TongThoiLuongPS = '" + ct.TongThoiLuongPS + "'"
                 + " set MaNSX = '" + ct.MaNSX + "'"
                 + " set MaLoaiCT = '" + ct.MaLoaiCT + "'"
                 + " set Status = '" + ct.Status + "'"
                 + " where MaCT =  '" + ct.MaCT + "'";
            return Update(sql);
        }
    }
}
